enum Direction {
  LEFT,
  RIGHT,
  BOTTOM,
  TOP;
}

class LocationUtil {
  static List<Direction> FOUR_USER_LOCATION = [
    Direction.BOTTOM,
    Direction.LEFT,
    Direction.TOP,
    Direction.RIGHT
  ];

  static List<Direction> THREE_USER_LOCATION = [
    Direction.BOTTOM,
    Direction.LEFT,
    Direction.TOP
  ];

  static List<Direction> TWO_USER_LOCATION = [Direction.BOTTOM, Direction.TOP];

  /// 定位用户所在位置
  /// @userCount  当前桌多少个人
  /// @current    当前用户所在序号
  /// @other      其他用户
  /// @location   当前用户所在 位置 默认为 BOTTOM
  /// 假设  序列号为 3 胡了 2号玩家的牌怎么办
  /// LocationUtil.buildLocationUtil(4, 3, 2, location: Direction.TOP);
  static Direction buildLocationUtil(int userCount, int current, int other,
      {Direction? location}) {
    location ??= Direction.BOTTOM;
    switch (userCount) {
      case 2:
        int userIndex = TWO_USER_LOCATION.indexOf(location);
        location =
            TWO_USER_LOCATION[_shift(userCount, current, other, userIndex)];
        break;
      case 3:
        int userIndex = THREE_USER_LOCATION.indexOf(location);
        location =
            THREE_USER_LOCATION[_shift(userCount, current, other, userIndex)];
        break;
      case 4:
        int userIndex = FOUR_USER_LOCATION.indexOf(location);
        location =
            FOUR_USER_LOCATION[_shift(userCount, current, other, userIndex)];
        break;
    }
    return location;
  }

  static int getRange(int sourceIndex, Direction target) {
    int index = 0;
    while (FOUR_USER_LOCATION[sourceIndex] != target) {
      sourceIndex++;
      if (sourceIndex > FOUR_USER_LOCATION.length - 1) {
        sourceIndex = 0;
      }
      index++;
    }
    return index;
  }

  /// 增加
  /// current             1-4

  static int _shift(int count, int current, int number, int userIndex) {
    var maxWhile = count + 1;
    while (current != number && maxWhile > 0) {
      current++;
      userIndex++;
      if (userIndex > count - 1) {
        userIndex = 0;
      }

      if (current > count) {
        current = 1;
      }
      maxWhile--;
    }
    return userIndex;
  }

  /// 获取图片地址
  /// 指向地址
  static Map<Direction, String> cardPointToImage = {
    Direction.TOP: "direction/top.png",
    Direction.BOTTOM: "direction/bottom.png",
    Direction.RIGHT: "direction/right.png",
    Direction.LEFT: "direction/left.png",
  };

  /// 左右都经过旋转 所以这里不能通过指向谁就返回谁
  static Direction? getCardPointTo(
      Direction? cardDirection, Direction? pointToDirection) {
    Direction? direction;
    if (cardDirection == null || pointToDirection == null) {
      return null;
    }
    // 左边出牌区域  右旋90°
    if (cardDirection == Direction.LEFT) {
      switch (pointToDirection) {
        case Direction.TOP:
          direction = Direction.LEFT;
          break;
        case Direction.BOTTOM:
          direction = Direction.RIGHT;
          break;
        case Direction.RIGHT:
          direction = Direction.TOP;
          break;
        case Direction.LEFT:
          direction = Direction.BOTTOM;
          break;
        default:
          direction = Direction.TOP;
      }
      // 右边出牌区域  右旋270°
    } else if (cardDirection == Direction.RIGHT) {
      switch (pointToDirection) {
        case Direction.TOP:
          direction = Direction.RIGHT;
          break;
        case Direction.BOTTOM:
          direction = Direction.LEFT;
          break;
        case Direction.RIGHT:
          direction = Direction.BOTTOM;
          break;
        case Direction.LEFT:
          direction = Direction.TOP;
          break;
        default:
          direction = Direction.TOP;
      }
    } else {
      direction = pointToDirection;
    }
    return direction;
  }
}
